#ifndef _YATAK_SOCKET_H
#define _YATAK_SOCKET_H

#define SOCKET_CONNECTED	4
#define SOCKET_CONNECTING	8
#define SOCKET_W_SYN_ACK	16

struct s_sockaddr_in
{
	unsigned int sa_family;
	unsigned int sa_port;
	unsigned int sa_addr;
};

struct s_socket_packet
{
	struct s_sockaddr_in saddr;
	struct s_net_buff *data;
};

struct s_socket
{
	unsigned int family;          // socketfamily (e.g. AF_INET, AF_IPX
	unsigned int type;            // type of socket (e.g. TCP, UDP)
	unsigned int port;            // port of socket (e.g. 23)
	
	unsigned int pa_addr;         // protocol address (e.g. 130.0.0.2)
	
	int receive_pid;              // pid of task who waits to receive
	unsigned int owner;           // pid of task who connects to socket
	
	unsigned int fd;              // file descriptor
	
	unsigned int state;
	
	struct list *data;            // socket_packets list of received data
	
	unsigned char *buffer;        // pointer to buffer
	
	struct s_sockaddr_in saddr;
	unsigned int seq;
};

/* get a socket by his datas */
int socket_get_fd(struct s_sockaddr_in *saddr);
/* get a socket by his fd */
struct s_socket* socket_get(unsigned int fd);
int socket_connected(struct s_socket *socket, int result);
int socket_received(struct s_sockaddr_in *saddr, struct s_sockaddr_in *daddr, unsigned char* buffer, unsigned int size);

/* Create a socket for a process */
int socket(unsigned int family, unsigned int type, unsigned int protocol, unsigned int pid);
int socket_bind(unsigned int sock, struct s_sockaddr_in *saddr, int size);
int socket_recvfrom(unsigned int pid, unsigned int sock, unsigned char*buffer, unsigned int size, struct s_sockaddr_in *saddr);
int socket_sendto(unsigned int sock, unsigned char*buffer, unsigned int size, struct s_sockaddr_in *daddr);
int socket_connect(unsigned int sock, struct s_sockaddr_in *daddr);
/* register a socket family */
int register_family(unsigned int new_family);

#endif
